﻿using System;
using System.Windows;
using System.Windows.Controls.Primitives;

namespace Gmantis.Controls
{
    public class TreeViewExpandButton : ToggleButton
    {
        // Fields
        internal static readonly DependencyProperty ShowLinesProperty = DependencyProperty.Register("ShowLines", typeof(bool), typeof(TreeViewExpandButton), new PropertyMetadata(false, new PropertyChangedCallback(TreeViewExpandButton.ShowLinesChanged)));

        // Methods
        protected void ChangeVisualStateShowIcon(bool useTransitions)
        {
            if (this.ShowLines)
            {
                VisualStateHelper.GoToState(this, "ShowPlusIcon", useTransitions);
            }
            else
            {
                VisualStateHelper.GoToState(this, "ShowTriangleIcon", useTransitions);
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.ChangeVisualStateShowIcon(false);
        }

        private static void ShowLinesChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
        {
            (d as TreeViewExpandButton).ChangeVisualStateShowIcon(true);
        }

        // Properties
        internal bool ShowLines
        {
            get { return (bool)GetValue(ShowLinesProperty); }
            set { SetValue(ShowLinesProperty, value); }
        }
    }
}
